<template>
	<view class="address">
		<!-- 收获地址 -->
		<view class="address-item">
			<p>
				联系人:
				<template v-if="rname">
					<span>{{rname}}</span>
				</template>
				<template v-else>
					暂无
				</template>
				<u-icon name="edit-pen" size="36" @click="goUser"></u-icon>
			</p>
			<p>
				联系电话:
				<template v-if="phone">
					<span>{{phone}}</span>
				</template>
				<template v-else>
					暂无
				</template>
				<u-icon name="edit-pen" size="36" @click="goUser"></u-icon>
			</p>
			<p>
				联系地址:
				<template v-if="addr">
					<span>{{addr}}</span>
				</template>
				<template v-else>
					暂无
				</template>
				<u-icon name="edit-pen" size="36" @click="goUser"></u-icon>
			</p>
			<p>
				配送费:
				<span>￥{{enterprise.sendcost}}</span>
			</p>
			<p>
				订单金额:
				<span>￥{{allTotal}}(不包含配送费)</span>
			</p>
			<p>
				总计:
				<span>￥{{Number(allTotal + enterprise.sendcost).toFixed(2)}}元</span>
			</p>
		</view>

		<!-- 下单按钮 -->
		<view class="btn">
			<button class="bottomButton" @click="showPayModal=true">下单</button>
		</view>

		<!-- 支付确认弹窗 -->
		<u-modal v-model="showPayModal" width="90%" :content="content" title="支付确认" :show-cancel-button="true" @confirm="pay"
		 ref="uModal" :async-close="true"></u-modal>
		 
		<wyb-loading ref="loading"/> 
	</view>
</template>
<script>
	import * as api from '@/api/apiList/user/user.js';
	import * as orApi from '@/api/apiList/user/order.js';
	import * as wxApi from '@/api/apiList/pay.js';

	import {
		createNamespacedHelpers
	} from 'vuex'
	const {
		mapState,
		mapMutations
	} = createNamespacedHelpers('unSignModule')

	import {
		formDate
	} from "@/utils/formDate.js"

	export default {
		props: {
			menus: { //菜单数据
				type: Object
			},
			enterprise: { //企业数据
				type: Object
			},
			member: { //用户信息
				type: Object
			},
			selectExptxt: { //选中的套餐
				type: Object
			},
		},
		data() {
			return {
				showPayModal: false
			}
		},
		computed: {
			...mapState(["shopCarMenuList"]),
			allTotal(){//总计
				let sum = 0
				this.shopCarMenuList.forEach((item)=>{
					sum += item.price
				})
				return sum
			},
			rname(){
				return this.$store.state.userModule.member.rname
			},
			phone(){
				return this.$store.state.userModule.member.phone
			},
			addr(){
				return this.$store.state.userModule.member.addr
			}
		},
		methods: {
			...mapMutations(["REMOVE_shopCarMenuList"]),
			async API_generateOrder(data) {
				//下单接口
				let result = {
					data
				};
				return await orApi.generateOrder(result);
			},
			async API_wxPay(oid) {
				//微信支付
				let result = {
					param: {
						oid //订单id
					}
				};
				return await wxApi.wxPay(result);
			},
			requestPayment(data) {
				//uni支付接口
				return new Promise((resolve, reject) => {
					uni.requestPayment({
						timeStamp: data.timeStamp,
						nonceStr: data.nonceStr,
						package: data.packageValue,
						signType: data.signType,
						paySign: data.paySign,
						success: function(res) {
							resolve(res);
						},
						fail: function(err) {
							reject(err);
						}
					});
				});
			},
			async pay() { //支付
				this.$refs.loading.showLoading() // 显示
				let date = formDate(new Date(this.menus.edate))//日期
				
				let data = {
					ordercosts: this.allTotal + this.enterprise.sendcost, //订单总额（包含配送费）
					ordercost: this.allTotal, //订单金额（不包含配送费）
					sendcost: this.enterprise.sendcost, //额定配送费
					type: this.menus.type, //类型 1-午餐、2-晚餐
					mnid: this.menus.id, //菜单id
					mndate: date, //菜单日期
					items: this.shopCarMenuList //订单子项
				}
				let res = await this.API_generateOrder(data)//下单
				
				console.log(res)
				if(res.data.code === 1000){//下单成功
					let payRes = await this.API_wxPay(res.data.data.id)
					
					if(payRes.data.code === 1000){//api支付成功
						try{
							let pay = await this.requestPayment(payRes.data.data)
							console.log(pay)
							if(pay.errMsg === "requestPayment:ok"){
								this.$parent.$refs.uToast.show({
									title: "支付成功",
									type: 'success',
									url: '/pages/user/components/set/components/order/order?statusCurrent=1'
								})
								this.showPayModal = false
								this.REMOVE_shopCarMenuList()//清空购物车
							}
						}catch(e){
							this.$parent.$refs.uToast.show({
								title: "已取消支付,请到我的订单页面查看未付款订单。",
								type: 'error',
								url: '/pages/user/components/set/components/order/order?statusCurrent=0'
							})
							this.showPayModal = false
							this.REMOVE_shopCarMenuList()//清空购物车
						}
					}
	
				}else{//下单不成功
					this.$parent.$refs.uToast.show({
						title: res.data.msg,
						type: 'error',
					})
					this.showPayModal = false
				}
				
				this.$refs.loading.hideLoading() // 隐藏
			},
			goUser(){
				uni.switchTab({
				    url: '/pages/user/user'
				});
			}
		}
	}
</script>
<style lang="scss" scoped>
	.address {
		position: relative;
		padding-top: 44rpx;
		position: fixed;
		bottom: 50px;
		width: 100%;
		background: #fff;
		border-top-left-radius: 20rpx;
		border-top-right-radius: 20rpx;
		border-top: 1px solid;

		.address-item {
			//收获地址样式
			width: 100%;
			line-height: 50rpx;
			float: left;
			padding-left: 30rpx;
			font-size: 32rpx;
			font-weight: 300;
			padding-bottom: 20rpx;

			p {
				color: #000000;
			}

			span {
				//其他三个信息的字体颜色
				color: #666666;
				margin-right: 20rpx;
				margin-left: 15rpx;
			}

			p:last-child span {
				//总计价格字体颜色
				color: #e44c4c;
			}
		}

		.btn {

			.bottomButton {
				background: #f43a3d;
				border-radius: 50rpx;
				color: #ffffff;
				letter-spacing: 1rpx;
				font-size: 30rpx;
			}
		}
	}
</style>
